package com.dgut.usermodule.web;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dgut.common.util.ExtAjaxResponse;
import com.dgut.common.util.ExtPageable;
import com.dgut.usermodule.entity.PermListDTO;
import com.dgut.usermodule.entity.PermSaveDTO;
import com.dgut.usermodule.entity.PermUpdateDTO;
import com.dgut.usermodule.entity.Permission;
import com.dgut.usermodule.service.PermService;

@RestController
@RequestMapping("/perm")
public class PermissionCtrl {
	@Autowired 
	private PermService service;
	/**
	 * 资源列表，支持模糊、分页查询
	 * @param dto
	 * @param pageable
	 * @return
	 */
	@RequestMapping("/list")
	public Page<Permission> list(PermListDTO dto,ExtPageable pageable){
		return service.findAll(PermListDTO.getSpecification(dto), pageable.getPageable());
	}
	@RequestMapping("/delete")
	public boolean Delete(@RequestParam Long id) {
		return service.delete(id);
	}
	/**
	 * 传入资源信息，保存该信息
	 * @param dto
	 * @param result
	 * @return
	 */
	@RequestMapping("/save")
	public String saved(@Validated PermSaveDTO dto,BindingResult result) {
		String res;
		if (result.hasErrors()) {
			List<ObjectError> errors = result.getAllErrors();
			res = errors.get(0).getDefaultMessage();
		}else {
			Permission perm = new Permission();
			perm.setIdentifier(dto.getIdentifier());
			perm.setUrl(dto.getUrl());
			perm.setUrlName(dto.getUrlName());
			perm.setParentTree(dto.getParentTree());
			if (service.save(perm)) {
				res = "true";
			}else {
				res = "false";
			}
			
		}
		return res;
	}
	/**
	 * 传入资源参数，修改资源信息
	 * @param dto
	 * @param result
	 * @return
	 */
	@RequestMapping("/update")
	public String Update(@Validated PermUpdateDTO dto,BindingResult result) {
		String res;
		if (result.hasErrors()) {
			List<ObjectError> errors = result.getAllErrors();
			res = errors.get(0).getDefaultMessage();
		}else {
			Permission perm = new Permission();
			perm.setId(dto.getId());
			perm.setIdentifier(dto.getIdentifier());
			perm.setParentTree(dto.getParentTree());
			perm.setUrl(dto.getUrl());
			perm.setUrlName(dto.getUrlName());
			if (service.save(perm)) {
				res = "true";
			}else {
				res = "false";
			}
		}
		return res;
	}
	/**
	 * 传入id，查询该资源信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/findone")
	public Permission findOne(@RequestParam Long id) {
		return service.findOne(id);
	}
	/**
	 * 传入userId和permId进行授权
	 * @param userId
	 * @param permId
	 * @return
	 */
	@RequestMapping("/authorize")
	public boolean authorize(@RequestParam Long userId,@RequestParam Long permId) {
		if (service.authorize(userId, permId)>0) {
			return true;
		}
		return false;
	}
	/**
	 * 传入userId和permId进行撤销权限
	 * @param userId
	 * @param permId
	 * @return
	 */
	@RequestMapping("/cacelauthorize")
	public boolean cacelauthorize(@RequestParam Long userId,@RequestParam Long permId) {
		if (service.cacelauthorize(userId, permId)>0) {
			return true;
		}
		return false;
	}
	@RequestMapping("/reauthorize")
	public ExtAjaxResponse reauthorize(@RequestParam Long userId,@RequestParam Long[] permId) {
		service.reauthorize(userId, permId);
		return new ExtAjaxResponse(true, "操作成功！");
	}
}
